<template>
    <div class="question-select">
        <!-- 搜索区域 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
            <el-form-item label="题型" prop="questionType">
                <el-select v-model="queryParams.questionType" placeholder="请选择题型" clearable>
                    <el-option v-for="dict in dict.type.exam_question_type" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="题库" prop="outline">
                <el-input v-model="queryParams.outline" placeholder="请输入题库关键字" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 题目列表 -->
        <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="题型" align="center" prop="questionType" width="100">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.exam_question_type" :value="scope.row.questionType" />
                </template>
            </el-table-column>
            <el-table-column label="题库" align="center" prop="outline" width="180" show-overflow-tooltip />
            <el-table-column label="题目名称" align="left" prop="questionTitle" min-width="300" show-overflow-tooltip>
                <template slot-scope="scope">
                    <span style="margin-left: 10px;">{{ scope.row.questionTitle }}</span>
                </template>
            </el-table-column>
            <el-table-column label="分值" align="center" prop="questionScore" width="80" />
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 底部按钮 -->
        <div class="dialog-footer">
            <el-button type="primary" @click="confirmSelection">确 定</el-button>
            <el-button @click="cancelSelection">取 消</el-button>
        </div>
    </div>
</template>

<script>
import { listQuestion } from "@/api/exam/question";

export default {
    name: "QuestionSelect",
    dicts: ['exam_question_type'],
    props: {
        selectedQuestions: {
            type: Array,
            default: () => []
        }
    },
    data() {
        return {
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 总条数
            total: 0,
            // 题目列表数据
            questionList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                questionType: undefined,
                outline: undefined
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询题目列表 */
        getList() {
            this.loading = true;
            listQuestion(this.queryParams).then(response => {
                this.questionList = response.rows;
                this.total = response.total;
                this.loading = false;
                // 设置已选中的题目
                if (this.selectedQuestions.length > 0) {
                    this.$nextTick(() => {
                        const selection = this.questionList.filter(item =>
                            this.selectedQuestions.some(selected => selected.id === item.id)
                        );
                        selection.forEach(row => {
                            this.$refs.table.toggleRowSelection(row, true);
                        });
                    });
                }
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 选择题目复选框 */
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.selectedRows = selection;
        },
        /** 确认选择 */
        confirmSelection() {
            this.$emit('confirm', this.selectedRows);
        },
        /** 取消选择 */
        cancelSelection() {
            this.$emit('cancel');
        }
    }
};
</script>

<style lang="scss" scoped>
.question-select {
    .dialog-footer {
        margin-top: 20px;
        text-align: center;
    }
}
</style>